package com.abe.jmcl.utils;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;

import com.abeframe.core.ConstantCore;
import com.abeframe.core.utils.Utils;

import java.io.File;
import java.io.FileWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class NormalUtils {

    //获取颜色
    public static int getColorRes(int res) {
        return Utils.getContext().getResources().getColor(res);
    }

    //获取字符串
    public static String getString(int res) {
        return Utils.getContext().getResources().getString(res);
    }

    //控制台输出
    public static void w(String message) {
        System.out.println(message);
    }

    //打开浏览器
    public static void openBrowser(Context context, String url) {
        if (TextUtils.isEmpty(url)) return;
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
    }

    //文件日志
    public static void writeFileToSD(String message) {
        if (TextUtils.isEmpty(message)) {
            return;
        }
        String sdStatus = Environment.getExternalStorageState();
        if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
            return;
        }
        try {
            File path = new File(ConstantCore.LOG_PATH);
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS", Locale.CHINA);
            String fileName = sf.format(calendar.getTime()) + ".txt";
            File file = new File(ConstantCore.LOG_PATH + File.separator + fileName);
            if (!path.exists()) {
                path.mkdirs();
            }
            if (!file.exists()) {
                file.createNewFile();
            }
            StringBuilder sBuilder = new StringBuilder();
            sBuilder.append(df.format(calendar.getTime()));
            sBuilder.append(":");
            sBuilder.append(message);
            FileWriter fw = new FileWriter(file, true);
            fw.write(sBuilder.toString());
            fw.write("\r\n");
            fw.flush();
            fw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
